//
//  LXSliderView.m
//  微博
//
//  Created by imera on 15/9/19.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXSliderView.h"
#import "UIImageView+WebCache.h"

@interface LXSliderView()

@property (nonatomic, strong) NSString *firstImage;
@property (nonatomic, assign) int imageCount;
/** 轮播图 */
@property (nonatomic, strong) NSArray *imageArr;

@end

@implementation LXSliderView

- (void)setVideoArr:(NSArray *)videoArr
{
    if (videoArr != nil) {
        _videoArr = videoArr;
    }
    
    for (int i=0; i<self.imageArr.count; i++) {
        UIImageView *imageView = self.imageArr[i];
        LXVideo *video = self.videoArr[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:video.thumb]];
    }

}

- (UIPageControl *)pageControl
{
    if(_pageControl == nil){
        
        _pageControl = [[UIPageControl alloc]init];
        
        _pageControl.currentPageIndicatorTintColor = LXColor(253, 98, 42);
        _pageControl.pageIndicatorTintColor = LXColor(189, 189, 189);
        //关闭用户交互
        _pageControl.userInteractionEnabled = NO;
    }
    
    return _pageControl;
}

/**
 *  快速创建轮播视图
 *
 *  @param firstImage 第一张轮播图片
 *  @param imageCount 所有的轮播图片数
 *  @param target 代理
 *
 */
- (instancetype)initWithFirstImage:(NSString *)firstImage imageCount:(int)imageCount
{
    self.firstImage = firstImage;
    self.imageCount = imageCount;
    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self){
        NSMutableArray *imageArr = [NSMutableArray array];
        //设置滚动图片
        for (int index = 0; index<self.imageCount; index++){
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.userInteractionEnabled = YES;
            //获取相同的图片名
            NSString *str = self.firstImage;
            NSString *sameName = [str substringWithRange:NSMakeRange(0, str.length-1)];
            
            NSString *imageName = [NSString stringWithFormat:@"%@%d",sameName,index + 1];
            imageView.image = [UIImage imageNamed:imageName];
            
            imageView.tag = index;
            
            [self addSubview:imageView];
            [imageArr addObject:imageView];
            
            UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
            [imageView addGestureRecognizer:tapGest];
        }
        self.imageArr = imageArr;
        
        //隐藏底部滚动条
        self.showsHorizontalScrollIndicator = NO;
        //开启页面识别
        self.pagingEnabled = YES;
        //去掉弹簧效果
        self.bounces = NO;
    }
    return self;

}

- (void)tap:(UITapGestureRecognizer *)tapGest
{
    UIImageView *imageView = (UIImageView *)tapGest.view;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"moviePlay" object:nil userInfo:@{@"video":self.videoArr[imageView.tag]}];
}

- (void)layoutSubviews
{
    //设置滚动范围
    self.contentSize = CGSizeMake(self.frame.size.width * self.imageCount, 0);
    	
    for (int index = 0; index<self.imageCount; index++) {
        UIImageView *imageView = self.subviews[index];
        
        //设置图片的frame
        CGFloat imageViewY = 0;
        CGFloat imageViewW = self.frame.size.width;
        CGFloat imageViewH = self.frame.size.height;
        CGFloat imageViewX = imageViewW * index;
        imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
        
    }
    
    //设置pageControl的frame
    _pageControl.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.9);
    _pageControl.numberOfPages = self.imageCount;
    _pageControl.bounds = CGRectMake(0, 0, self.frame.size.width, 30);
}
@end
